package kuangshen.dead;

/**
 * @program: liangt
 * @description: 死锁
 * @author: liangt
 * @create: 2021-03-03 15:58
 */
public class DeadLock {

    public static void main(String[] args) {
        MakeUp makeUp1 = new MakeUp(0,"王雨");
        MakeUp makeUp2 = new MakeUp(1,"胖墩");
        makeUp1.start();
        makeUp2.start();
    }
}

//口红
class Lipstick{

}
//镜子
class Mirror{

}

class MakeUp extends Thread{
    static Mirror mirror = new Mirror();
    static Lipstick lipstick = new Lipstick();

    int codeTime;
    String name;

    public MakeUp(int codeTime,String name){
        this.codeTime = codeTime;
        this.name = name;
    }

    @Override
    public void run() {
        super.run();
    }

    public void makeUP(){
        if (codeTime == 0){
            synchronized(mirror){
                System.out.println(this.name+"获取镜子");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lipstick){
                    System.out.println(this.name+"获得口红");

                }
            }
        }else {
            synchronized(lipstick){
                System.out.println(this.name+"获取口红");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (mirror){
                    System.out.println(this.name +"获得镜子");
            }
        }
        }
    }

}